package org.example.controller;


import cn.hutool.extra.tokenizer.Result;
import cn.hutool.extra.tokenizer.Word;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.example.pojo.Employee;
import org.example.service.EmployeeService;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
@Api(tags = "测试员工功能")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    //--分页显示全部员工列表-------------------------------------------------------------------------------
    @GetMapping("/showEmployees/{page}/{limit}")
    @ResponseBody
    @ApiOperation("分页连表查询所有的员工")
    public ResponseData showEmployees(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据") @PathVariable int limit){
        //使用PageHelper开启分页功能
         PageHelper.startPage(page,limit);
        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Employee> employees = employeeService.showEmployees();
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        ResponseData responseData = ResponseData.getJsonData(employees,(int)employeePageInfo.getTotal());
        return responseData;
    }


    //--批量删除员工----------------------------------------------------------------------------------------
    @DeleteMapping("/deleteMany/{eIds}")
    @ResponseBody
    @ApiOperation("批量删除员工")
    public ResponseData deleteEmployees(@ApiParam("要删除的id字符串") @PathVariable String eIds){
        String[] ids = eIds.split(",");
        int num = employeeService.deleteEmployees(ids);
        if(num>0){
            return ResponseData.getSccesss("批量删除成功",100);
        }else{
            return ResponseData.error("批量删除失败",101);
        }
    }


    //--单个删除员工-------------------------------------------------------------------------------------------
    @DeleteMapping("/deleteOne/{eId}")
    @ResponseBody
    @ApiOperation("单个删除员工")
    public ResponseData deleteEmployee(@ApiParam("要删除的id") @PathVariable int eId){
        int num = employeeService.deleteEmployee(eId);
        if(num>0){
            return ResponseData.getSccesss("删除成功",100);
        }else{
            return ResponseData.error("删除失败",101);
        }

    }


    //--添加员工---------------------------------------------------------------------------------------------
    @PostMapping("/addEmployee")
    @ResponseBody
    @ApiOperation("添加员工")
    public ResponseData addEmployee(@ApiParam("添加的员工信息")  Employee employee){
        int num = employeeService.addEmployee(employee);
        if(num>0){
            return ResponseData.getSccesss("添加成功",100);
        }else{
            return ResponseData.error("添加失败",101);
        }
    }


    //--修改员工信息-------------------------------------------------------------------------------------------
    @PostMapping("/updateEmployee")
    @ResponseBody
    @ApiOperation("修改员工")
    public ResponseData updateEmployee(@ApiParam("修改的员工信息") Employee employee){
       int num = employeeService.updateEmployee(employee);
        if(num>0){
            return ResponseData.getSccesss("修改成功",100);
        }else{
            return ResponseData.error("修改失败",101);
        }
    }


  //--回显员工数据（根据id查询员工）-----------------------------------------------------------------------------
  @GetMapping("/showEmployeeById/{eId}")
  @ResponseBody
  @ApiOperation("回显员工数据")
  public ResponseData showEmployeeById(@ApiParam("要回显的员工的id") @PathVariable int eId){
        Employee employee = employeeService.showEmployeeById(eId);
        ResponseData responseData = ResponseData.getJsonData(employee,1);
        return responseData;
  }


  //--多表连接模糊查询全部员工列表--------------------------------------------------------------------------------------------
    @GetMapping("/showEmployeesByAll/{page}/{limit}/{message}")
    @ResponseBody
    @ApiOperation("连表模糊查询全部员工列表")
    public ResponseData showEmployeesByAll(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据") @PathVariable int limit,@ApiParam("模糊查询输入的内容") @PathVariable  String message){
        PageHelper.startPage(page,limit);
        System.out.println(message);
        if("undefined".equals(message)) {
            message = null;
        }
        else if(" ".equals(message)){
            message = null;
        }
        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Employee> employees = employeeService.showEmployeesByAll(message);
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        ResponseData responseData = ResponseData.getJsonData(employees,(int)employeePageInfo.getTotal());
        return responseData;
    }


 //--根据员工是否在职离职状态查询员工-----------------------------------------------------------------------------------------------
    @GetMapping("/showZaiZhiEmployees/{page}/{limit}/{msg}")
    @ResponseBody
    @ApiOperation("根据员工是否在职离职状态查询员工")
 public ResponseData showZaiZhiEmployees(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据")
    @PathVariable int limit,@ApiParam("模糊查询输入的内容") @PathVariable  String msg){
        PageHelper.startPage(page,limit);
        List<Employee> employees = employeeService.showZaiZhiEmployees(msg);
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        ResponseData responseData = ResponseData.getJsonData(employees,(int)employeePageInfo.getTotal());
        return responseData;
 }


 //--多表连接模糊查询在职员工列表-----------------------------------------------------------------------------------------

    @GetMapping("/showZaiZhiEmployeesByAll/{page}/{limit}/{message}/{msg}")
    @ResponseBody
    @ApiOperation("连表模糊查询在职员工列表")
    public ResponseData showZaiZhiEmployeesByAll(
            @ApiParam("当前页") @PathVariable int page,
            @ApiParam("每一页显示几条数据") @PathVariable int limit,@ApiParam("模糊查询输入的内容") @PathVariable String message,
                                                 @ApiParam("在职的状态") @PathVariable  String msg){
        PageHelper.startPage(page,limit);
        System.out.println(message);
        if("undefined".equals(message)) {
            message = null;
        }
        else if(" ".equals(message)){
            message = null;
        }
        Map<String,String> map = new HashMap<>();
        map.put("message",message);
        map.put("msg",msg);
        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Employee> employees = employeeService.showZaiZhiEmployeesByAll(map);
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        ResponseData responseData = ResponseData.getJsonData(employees,(int)employeePageInfo.getTotal());
        return responseData;
    }


    //--连表模糊查询离职员工列表或离职员工黑名单------------------------------------------------------------------------------
    @GetMapping("/showLiZhiEmployeesByAll/{page}/{limit}/{message}/{msg}")
    @ResponseBody
    @ApiOperation("连表模糊查询离职员工列表")
    public ResponseData showLiZhiEmployeesByAll(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据") @PathVariable int limit,@ApiParam("模糊查询输入的内容") @PathVariable String message,
                                                 @ApiParam("在职的状态") @PathVariable  String msg){
        PageHelper.startPage(page,limit);
        System.out.println(message);
        if("undefined".equals(message)) {
            message = null;
        }
        else if(" ".equals(message)){
            message = null;
        }
        Map<String,String> map = new HashMap<>();
        map.put("message",message);
        map.put("msg",msg);
        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Employee> employees = employeeService.showLiZhiEmployeeByAll(map);
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        ResponseData responseData = ResponseData.getJsonData(employees,(int)employeePageInfo.getTotal());
        return responseData;
    }

    //--导出到数据到excel--------------------------------------------------------------------------------------
    @GetMapping("/employeeExcel")
    @ApiOperation("导出员工数据")
    public void employeeExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        // 设置字符集
        response.setCharacterEncoding("utf-8");
        // 防止中文乱码
        final String FILENAME ="employee";
        // 设置 header
        response.setHeader("Content-disposition", "attachment;filename=" + FILENAME + ".xlsx");
        // 写入
        EasyExcel.write(response.getOutputStream(), Employee.class)
                .sheet("员工信息")// 指定sheet名称
                .doWrite(employeeService.selectAllEmployees());

    }

  @PostMapping("/fileExcel")
  @ResponseBody
  public ResponseData fileExcel(MultipartFile fileExcel){
      try {
          List<Employee> list = EasyExcel.read(fileExcel.getInputStream()).head(Employee.class).sheet().
                  doReadSync();
         list.forEach(employee -> employeeService.addEmployee(employee));

          return    ResponseData.getSccesss("成功",100);
      } catch (IOException e) {
          return ResponseData.getSccesss("失败",101);
      }

  }




}
